package hot100.logos91To100;

/**
 * @author logos
 * date 2025/7/19 12:56
 * @version v1.0
 * @Package hot100.logos91To100
 */
public class logos98 {

    /**
     * 98. 颜色分类
     * https://leetcode.cn/problems/sort-colors/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution1 {
        public void sortColors(int[] nums) {
            int red = 0;
            int white = 0;
            int blue = 0;
            for (int x : nums) {
                if (x == 0) {
                    red++;
                } else if (x == 1) {
                    white++;
                } else {
                    blue++;
                }
            }
            for (int i = 0; i < nums.length; i++) {
                if (i < red) {
                    nums[i] = 0;
                } else if (i < red + white) {
                    nums[i] = 1;
                } else if (i < red + white + blue) {
                    nums[i] = 2;
                }
            }
        }
    }

    class Solution2 {
        public void sortColors(int[] nums) {
            int red = 0;
            int white = 0;
            for (int i = 0; i < nums.length; i++) {
                int x = nums[i];
                nums[i] = 2;
                if (x <= 1) {
                    nums[white++] = 1;
                }
                if (x == 0) {
                    nums[red++] = 0;
                }
            }
        }
    }
}
